<template>
  <el-dialog :title="title" :visible="isShow" @close="closeEvent">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px">
      <el-form-item label="权限名称" prop="name">
        <el-input v-model="form.name" />
      </el-form-item>
      <el-form-item label="权限标识" prop="code">
        <el-input v-model="form.code" />
      </el-form-item>
      <el-form-item label="权限描述" prop="description">
        <el-input v-model="form.description" />
      </el-form-item>
      <el-form-item label="企业可见" prop="enVisible">
        <el-switch
          v-model="form.enVisible"
          active-text="可见"
          inactive-text="不可见"
          active-value="0"
          inactive-value="1"
        />
      </el-form-item>
    </el-form>
    <template>
      <div style="text-align: center">
        <el-button @close="closeEvent">取消</el-button>
        <el-button type="primary" @click="submit">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script>
import { sysPermissionPost, sysPermissionEdit } from '@/api/permission'
export default {
  name: '',
  data() {
    return {
      mode: 'add',
      isShow: false,
      form: {
        name: '',
        code: '',
        description: '',
        enVisible: '0',
        type: 1,
        pid: '0'
      },
      rules: {
        name: [
          { required: true, message: '请输入权限名称', trigger: 'change' }
        ],
        code: [
          { required: true, message: '请输入权限标识', trigger: 'change' }
        ],
        description: [
          { required: true, message: '请输入权限描述', trigger: 'change' }
        ],
        enVisible: [{ required: true, message: '必填', trigger: 'change' }]
      }
    }
  },
  computed: {
    title() {
      return {
        add: '新增权限',
        edit: '编辑权限'
      }[this.mode]
    }
  },
  created() {},
  methods: {
    submit() {
      this.$refs.form.validate(async(valid) => {
        if (valid) {
          if (this.mode === 'add') {
            await sysPermissionPost(this.form)
            this.$message.success('新增成功')
          } else if (this.mode === 'edit') {
            await sysPermissionEdit(this.form)
            this.$message.success('编辑成功')
          }
          this.closeEvent()
          this.$emit('getData')
        }
      })
    },
    closeEvent() {
      this.isShow = false
      this.$refs.form.resetFields()
      this.form = {
        name: '',
        code: '',
        description: '',
        enVisible: '0',
        type: 1,
        pid: '0'
      }
    }
  }
}
</script>
<style lang="less" scoped></style>
